package com.avocado.bt.impl.decorator;

import com.avocado.bt.BTState;
import com.avocado.bt.IBTContext;
import com.avocado.bt.IBTNode;
import com.avocado.bt.abs.decorator.DecoratorNode;

/**
 * 重复执行的节点
 *
 * @Author: zsh
 * @Date: 2023/10/17 9:31
 */
public class RepeatDecoratorNode extends DecoratorNode {

    // 限制执行次数
    private final int limitTimes;

    public RepeatDecoratorNode(String param) {
        super(param);
        limitTimes = Integer.parseInt(param);
    }

    @Override
    public BTState doUpdate(IBTContext context) {
        if (getChildNode() == null) {
            return BTState.Failure;
        }
        IBTNode childNode = getChildNode();
        for (int i = 0; i < limitTimes; i++) {
            childNode.tick();
            if (childNode.getState() == BTState.Failure) {
                return BTState.Failure;
            }
        }
        return BTState.Completed;
    }

}
